package com.wolfalpha.utron.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.wolfalpha.utron.R;

import java.util.List;

/**
 * 自定义的对话框
 *
 * @author
 */
public class ListAlertDialog {
    private Context context;
    private android.app.AlertDialog ad;
    private TextView titleView;
    private RelativeLayout messageView;
    private TextView sure;
    private TextView cancel;
    private ListView lvDia;

    private LayoutInflater inflater;

    public ListAlertDialog(Context context) {
        // TODO Auto-generated constructor stub
        this.context = context;
        inflater = LayoutInflater.from(context);
        ad = new android.app.AlertDialog.Builder(context).create();
        ad.show();
        //关键在下面的两行,使用window.setContentView,替换整个对话框窗口的布局
        Window window = ad.getWindow();
        window.setContentView(R.layout.dialog_list);
        titleView = (TextView) window.findViewById(R.id.tv_dialog_base_title);
        messageView = (RelativeLayout) window.findViewById(R.id.rl_dialog_base_message);
        lvDia = (ListView) window.findViewById(R.id.lv_dialog);
        sure = (TextView) window.findViewById(R.id.tv_dialog_base_sure);
        cancel = (TextView) window.findViewById(R.id.tv_dialog_base_cancel);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    public void setTitle(String title) {
        titleView.setText(title);
    }


    /**
     * 设置适配器
     *
     * @param adatpter
     */
    public void setAdatpter(BaseAdapter adatpter) {
        lvDia.setAdapter(adatpter);
        lvDia.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) mListener.finish(position);
            }
        });
    }

    public void setList(List<String> arrs) {
        ArrayAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrs.toArray(new String[]{}));
        lvDia.setAdapter(adapter);

        lvDia.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) mListener.finish(position);
                dismiss();
            }
        });
    }

    public void setArray(String[] arrs) {
        ArrayAdapter adapter = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_1, arrs);
        lvDia.setAdapter(adapter);

        lvDia.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (mListener != null) mListener.finish(position);
                dismiss();
            }
        });
    }


    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setPositiveButton(String text, final View.OnClickListener listener) {
        sure.setText(text);
        sure.setOnClickListener(listener);
    }

    /**
     * 设置按钮
     *
     * @param text
     * @param listener
     */
    public void setNegativeButton(String text, final View.OnClickListener listener) {
        cancel.setText(text);
        cancel.setOnClickListener(listener);
    }

    /**
     * 关闭对话框
     */
    public void dismiss() {
        ad.dismiss();
    }

    private OnFinishedListener mListener;

    public void setOnFinishedListener(OnFinishedListener listener) {
        mListener = listener;
    }


    public interface OnFinishedListener {
        public void finish(int which);
    }
}